{% assign applovin_list = storeConfig.applovin_list | strtoarray %}
{% get_pixels_domain list={applovin_list} %}
{%- assign pixels = list -%}
{% if pixels %}
<script>
    var applovinList= {{pixels|json}};
    var applovinPixel = applovinList[0];
    var AXON_EVENT_KEY = applovinPixel.facebook_pixel_id;
    !function(e,r){var t=["https://s.axon.ai/pixel.js","https://c.albss.com/p/l/loader.iife.js"];if(!e.axon){var a=e.axon=function(){a.performOperation?a.performOperation.apply(a,arguments):a.operationQueue.push(arguments)};a.operationQueue=[],a.ts=Date.now(),a.eventKey=AXON_EVENT_KEY;for(var n=r.getElementsByTagName("script")[0],o=0;o<t.length;o++){var i=r.createElement("script");i.async=!0,i.src=t[o],n.parentNode.insertBefore(i,n)}}}(window,document);

    axon("init");

    document.addEventListener("DOMContentLoaded",function(){
     
      function pushPixel(){
        axon.apply(null,['track',...arguments])
      }

      pushPixel("page_view")

      function getaApplovinCategoryMap(){
          try {
            return JSON.parse(applovinPixel.remark).applovin_category_mapping
          } catch (error) {
            return []
          }
      }

      function getCategoryMap(type){
        const applovinCategoryMap = getaApplovinCategoryMap();
        if(applovinCategoryMap.length === 0){
          return null;
        }

        const currentItem = applovinCategoryMap.filter(item=>item.product_type === type);
        if(currentItem.length > 0){
          return currentItem[0].category_id
        }

        const defaultItem = applovinCategoryMap.filter(item=>item.product_type === "default");
        if(defaultItem.length > 0){
          return defaultItem[0].category_id
        }

        return null;
      }

      function getProductItem(product){
        return {
          item_variant_id:  product.variant_id || product.variant.id,
          item_id: product.spu || product.product_id,
          item_name: product.variant_title || product.variant.title,
          item_category_id:getCategoryMap(product.product_type),
          quantity: 1,
          price: product.price || product.variant.price,
          image_url:product.src || product.image.src,
        }
      }
      function getEcDataProductItem(product){
        return {
          item_variant_id: product.ec_data_id,
          item_id: product.ec_data_spu,
          item_name: product.ec_data_name,
          item_category_id: getCategoryMap(product.ec_data_category),
          quantity: product.ec_data_quantity,
          price: product.ec_data_price,
          image_url:product.ec_data_image
        }
      }

      window['TOP_GLOBAL_CALLBACK_LAYER'].register({
        view_content:function(){
          pushPixel("view_item",{
            currency: _GET_C_SETTING_('base_currency_code'),
            value:_GET_C_SETTING_('product').variant.price,
            items:[getProductItem(_GET_C_SETTING_('product'))]
          })
        },
        search:function(){
          pushPixel('search',{
            results:_GET_C_SETTING_('search_products').map(item=>{
              return getProductItem(item)
            }),
            search_term:'{{search.keywords | filter_string_format}}'
          })

        },
        purchase:function(){
            pushPixel('purchase', {
                transaction_id: _GET_C_SETTING_('cart').order_number,
                shipping:_GET_C_SETTING_('cart').current_shipping_price,
                tax:_GET_C_SETTING_('cart').current_tax_price,
                user_id:_GET_C_SETTING_('cart').customer_id,
                items:_GET_C_SETTING_('cart').products.map(item=>{
                    return getProductItem(item)
                }),
                {% if storeConfig.applovin_sales_goods_amount %}
                value: "{{ order.current_subtotal_price| plus: order.current_coupon_price| plus: order.current_promotion_price }}",
                {% else %}
                value: "{{ order.total_price }}",
                {% endif %}
                currency: "{{ order.currency_code }}",
            });
        },
        add_to_cart:function(ecdata){
          pushPixel('add_to_cart', {
            currency: _GET_C_SETTING_('base_currency_code'),
            value:ecdata['ec_data_price'] * ecdata['ec_data_quantity'],
            items:[getEcDataProductItem(ecdata)]
          });
        },
        remove_from_cart:function(ecdata){
          pushPixel('remove_from_cart', {
            currency: _GET_C_SETTING_('base_currency_code'),
            value:ecdata['ec_data_price'] * ecdata['ec_data_quantity'],
            items:[getEcDataProductItem(ecdata)]
          });
        },
        add_payment_information:function(){
          pushPixel('add_payment_info', {
            currency: _GET_C_SETTING_('base_currency_code'),
            value:_GET_C_SETTING_('cart').total_price,
            payment_type:"credit_card",
            contents:_GET_C_SETTING_('cart').items.map(item=>{
              return getProductItem(item.product)
            })
          });
        },
        begin_checkout:function(){
          pushPixel('begin_checkout', {
            currency: _GET_C_SETTING_('base_currency_code'),
            value:_GET_C_SETTING_('cart').total_price,
            items:_GET_C_SETTING_('cart').items.map(item=>{
                return getProductItem(item.product)
            })
          });
        },
        sign_up:function(){
          pushPixel('sign_up',{
            method:"email"
          });
        },
        login:function(){
          pushPixel('login');
        },
        generate_lead:function(){
          pushPixel('generate_lead');
        }
      })
    }) 
</script>
{% endif %}
